package com.lanou3g.demo.controller;

import com.lanou3g.demo.controller.params.AdminInfoPwdParams;
import com.lanou3g.demo.controller.params.AdminParams;
import com.lanou3g.demo.entity.AdminInfo;
import com.lanou3g.demo.entity.RoleInfo;
import com.lanou3g.demo.entity.impl.AdminInfoImpl;
import com.lanou3g.demo.service.IAdminInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.util.List;

@RestController
public class AdminInfoController {

    @Autowired
    private IAdminInfoService adminInfoService;

    /**
     * 查看个人信息
     *
     * @param session
     * @return
     */
    @RequestMapping("/selectAdminInfo.do")
    public AdminInfo selectAdminInfoWithRoleName(HttpSession session) {
        String name = (String) session.getAttribute("name");
        AdminInfo admin = new AdminInfo();
        admin.setName(name);
        return adminInfoService.selectAdminInfoWithRoleName(admin);
    }

    /**
     * 修改个人信息 姓名,电话,Email
     *
     * @param
     * @return
     */
    @RequestMapping(value = "/updateAdminInfo.do", method = RequestMethod.POST)
    public boolean updateAdminInfo(@RequestBody @Validated AdminInfo adminInfo, BindingResult result) {
        if (result.hasErrors()) {
            return false;
        } else {
            return adminInfoService.updateAdminInfo(adminInfo);
        }
    }

    /**
     * 修改密码
     *
     * @param params
     * @param oldAdminInfo
     * @param newAdminInfo
     * @param session
     * @return
     */
    @RequestMapping(value = "/updateAdminPwd.do", method = RequestMethod.POST)
    public boolean updateAdminPwd(
            @RequestBody @Validated AdminInfoPwdParams params,
            BindingResult result,
            AdminInfo oldAdminInfo,
            AdminInfo newAdminInfo,
            HttpSession session) {
        String name = (String) session.getAttribute("name");
        AdminInfo ai = new AdminInfo();
        ai.setName(name);
        if (result.hasErrors()) {
            return false;
        } else {
            oldAdminInfo.setPassword(params.getOldPwd());
            boolean o = adminInfoService.findOne(ai, oldAdminInfo);
            if (o) {
                newAdminInfo.setPassword(params.getNewPwd());
                newAdminInfo.setName(ai.getName());
                return adminInfoService.updateAdminPwd(newAdminInfo);
            } else {
                return false;
            }
        }
    }

    /**
     * 查看所有的管理员
     *
     * @return
     */
    @RequestMapping("/selectAllAdmin.do")
    public List<AdminInfoImpl> selectAllAdmin() {
        return adminInfoService.selectAllAdmin();
    }

    @RequestMapping("/selectRoleWithAdmin.do")
    public List<AdminInfoImpl> selectRoleWithAdmin(String roleName) {
        return adminInfoService.selectRoleWithAdmin(roleName);
    }

    /**
     * 增加管理员
     *
     * @param adminParams
     */
    @RequestMapping("/addAdmin.do")
    public boolean addAdmin(@RequestBody AdminParams adminParams) {
        try {
            return adminInfoService.addAdminWithRole(adminParams);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 修改管理员信息回显
     *
     * @param adminId
     * @return
     */
    @RequestMapping("modifyFindAdmin.do")
    public AdminParams modifyFindAdmin(Integer adminId) throws SQLException {
        AdminInfoImpl adminInfo = new AdminInfoImpl();
        adminInfo.setAdminId(adminId);
        return adminInfoService.modifyFindAdmin(adminInfo);
    }

    /**
     * 修改管理员信息
     *
     * @param adminParams
     * @return
     */
    @RequestMapping(value = "/modifyAdmin.do", method = RequestMethod.POST)
    public boolean modifyAdmin(@RequestBody AdminParams adminParams, String adminId) {
        try {
            if (adminParams.getRoleIds().isEmpty()) {
                return false;
            }
            if (adminParams.getAdminInfo().getName() == null || adminParams.getAdminInfo().getName().length() <= 0) {
                return false;
            }
            return adminInfoService.modifyAdmin(adminParams, adminId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除管理员
     *
     * @param adminId
     * @return
     */
    @RequestMapping("/deleteAdminWithRole.do")
    public boolean deleteAdminWithRole(String adminId) {
        try {
            return adminInfoService.deleteAdmin(adminId);
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 密码重置
     *
     * @param adminIds
     * @return
     */
    @RequestMapping("/resetPwd.do")
    public boolean resetPwd(@RequestBody List<String> adminIds) {
        try {
            if (adminIds.size() <= 0) {
                return false;
            }
            if (adminIds.contains("on")) {
                adminIds.remove("on");
            }
            int i = adminInfoService.resetPwd(adminIds);
            return i > 0;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }

    }
}
